package framework.common.widgets.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import hk.m4s.lr.repair.test.R;

public class ConfirmDialog extends Dialog {

	public static final int TEXTLEFT =1;
	public static final int TEXTRIGHT =2;
	private int gravity;
	private Context context;
	private String title;
	private String text;
	private String confirmButtonText;
	private String cacelButtonText;
	private ClickListenerInterface clickListenerInterface;
	
	private LinearLayout confirm_dialog_sure;
	private LinearLayout confirm_dialog_cancle;
	private TextView confirm_dialog_sure_text;
	private TextView confirm_dialog_cancle_text;
	private TextView confirm_dialog_text;
	private EditText confirm_dialog_edit;
	private LinearLayout confirm_dialog_title;
	private TextView confirm_dialog_title_text;
	private boolean editFlag=false;

	public interface ClickListenerInterface {

		public void doConfirm(String str);
		
		public void doCancel();
	}

	public ConfirmDialog(Context context, String text) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.text = text;
		initview();
	}
	
	public ConfirmDialog(Context context, String title,
			String confirmButtonText, String cacelButtonText,boolean editFlag) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.title = title;
		this.editFlag=editFlag;
		this.confirmButtonText = confirmButtonText;
		this.cacelButtonText = cacelButtonText;
		initview();
	}

	public ConfirmDialog(Context context, String title, String text,
			String confirmButtonText, String cacelButtonText) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.title = title;
		this.text = text;
		this.confirmButtonText = confirmButtonText;
		this.cacelButtonText = cacelButtonText;
		initview();
	}
	public ConfirmDialog(Context context, String title, String text) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.title = title;
		this.text = text;
		initview();
	}
	public ConfirmDialog(Context context, String title, String text,int gravity) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.title = title;
		this.text = text;
		this.gravity = gravity;
		initview();
	}

	public ConfirmDialog(Context context, String text,
			String confirmButtonText, String cacelButtonText) {
		super(context, R.style.report_dialog);
		this.context = context;
		this.text = text;
		this.confirmButtonText = confirmButtonText;
		this.cacelButtonText = cacelButtonText;
		initview();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		
	}

	public void initview() {
		this.setCanceledOnTouchOutside(false);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.confirm_dialog, null);
		setContentView(view);
		setTitle(null);

		 confirm_dialog_title = (LinearLayout) view
				.findViewById(R.id.confirm_dialog_title);
		 confirm_dialog_title_text = (TextView) view
				.findViewById(R.id.confirm_dialog_title_text);
		 confirm_dialog_sure = (LinearLayout) view
				.findViewById(R.id.confirm_dialog_sure);
		 confirm_dialog_cancle = (LinearLayout) view
				.findViewById(R.id.confirm_dialog_cancle);
		 confirm_dialog_sure_text = (TextView) view
				.findViewById(R.id.confirm_dialog_sure_text);
		 confirm_dialog_cancle_text = (TextView) view
				.findViewById(R.id.confirm_dialog_cancle_text);
		 confirm_dialog_text = (TextView) view
				.findViewById(R.id.confirm_dialog_text);
		 confirm_dialog_edit=(EditText) view.findViewById(R.id.confirm_dialog_edit);
		if (title == null || "".equals(title)) {
			confirm_dialog_title.setVisibility(View.GONE);
		} else {
			confirm_dialog_title_text.setText(title);
		}
		if(editFlag){
			confirm_dialog_edit.setVisibility(View.VISIBLE);
			confirm_dialog_text.setVisibility(View.GONE);
		}else{
			confirm_dialog_edit.setVisibility(View.GONE);
			confirm_dialog_text.setVisibility(View.VISIBLE);
		}
		if(gravity == TEXTLEFT){
			confirm_dialog_text.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
		}
		if(gravity == TEXTRIGHT){
			confirm_dialog_text.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
		}
		
		confirm_dialog_text.setText(text);
		
		if ((confirmButtonText != null && !"".equals(confirmButtonText))
				|| (cacelButtonText != null && !"".equals(cacelButtonText))) {
			confirm_dialog_sure_text.setText(confirmButtonText);
			confirm_dialog_cancle_text.setText(cacelButtonText);
		}
		confirm_dialog_sure.setOnClickListener(new clickListener());
		confirm_dialog_cancle.setOnClickListener(new clickListener());

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); 
		lp.width = (int) (d.widthPixels * 0.8); 
		// lp.height = (int) (d.widthPixels * 0.5); 
		dialogWindow.setAttributes(lp);
	}
	public void onlyconfirm(boolean b){
		if(b){
			confirm_dialog_cancle.setVisibility(View.GONE);
		}else{
			confirm_dialog_cancle.setVisibility(View.VISIBLE);
		}
	}
	public void onlycancle(boolean b){
		if(b){
			confirm_dialog_sure.setVisibility(View.GONE);
		}else{
			confirm_dialog_sure.setVisibility(View.VISIBLE);
		}
	}
	
	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	private class clickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = v.getId();
			switch (id) {
			case R.id.confirm_dialog_sure:
				clickListenerInterface.doConfirm(confirm_dialog_edit.getText().toString());
				break;
			case R.id.confirm_dialog_cancle:
				clickListenerInterface.doCancel();
				break;
			}
		}

	};

}